package com.hawthorn.admin.controller.sysdept;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hawthorn.admin.service.sysdept.SysDeptService;
import com.hawthorn.platform.annotation.MarkLog;
import com.hawthorn.platform.exception.BizCode;
import com.hawthorn.platform.utils.common.MyUtil;
import com.hawthorn.platform.utils.iassert.AssertMyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * <p>todo 部门维护controller</p>
 *
 * @version v1.0.1
 * @author: fengliye@expservice.com.cn
 * @created: 2022/6/22 16:51
 */
@Api(tags = {"admin-sysdept"})
@RestController
@RequestMapping("/sysdept")
@Slf4j
public class SysDeptController
{
  @Resource
  private SysDeptService sysDeptService;

  /**
   * <p>todo 查询部门信息</p>
   *
   * @param params
   * @return: com.baomidou.mybatisplus.core.metadata.IPage<java.util.Map < java.lang.String, java.lang.Object>>
   * @author: fengliye@expservice.com.cn
   * @date: 2022/6/22 16:55
   * @version: 1.0.1
   */
  @ApiOperation(value = "查询->全部部门信息", notes = "查询全部部门信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "params", value = "查询条件参数", dataTypeClass = String.class)
  })
  @PostMapping(value = "/listDepts")
  public IPage<Map<String, Object>> listDepts(@RequestBody String params)
  {
    Page<Map<String, Object>> page = new Page<>();
    String conditions = MyUtil.getConditionsWhere(params, page);
    return sysDeptService.listDepts(page, conditions);
  }

  /**
   * <p>todo 保存部门信息</p>
   *
   * @param param
   * @return: java.util.Map<java.lang.String, java.lang.Object>
   * @author: fengliye@expservice.com.cn
   * @date: 2022/6/23 15:54
   * @version: 1.0.1
   */
  @ApiOperation(value = "保存->保存部门信息", notes = "插入部门信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "params", value = "查询条件参数", dataTypeClass = String.class)
  })
  @MarkLog(value = "新增部门-[{0}]", param = "dname")
  @PostMapping(value = "/saveDept")
  public Map<String, Object> saveDept(@RequestBody Map<String, Object> param)
  {
    return sysDeptService.saveDept(param);
  }

  /**
   * <p>todo 更新部门信息</p>
   *
   * @param id
   * @param param
   * @return: java.util.Map<java.lang.String, java.lang.Object>
   * @author: fengliye@expservice.com.cn
   * @date: 2022/6/23 15:56
   * @version: 1.0.1
   */
  @ApiOperation(value = "更新->更新部门信息", notes = "更新部门信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "主键", dataTypeClass = Long.class),
      @ApiImplicitParam(name = "param", value = "更新参数", dataTypeClass = Map.class)
  })
  @MarkLog(value = "更新部门-[{0}]", param = "dname")
  @PutMapping(value = "/updateDept/{id}")
  public Map<String, Object> updateDept(@PathVariable("id") Long id, @RequestBody Map<String, Object> param)
  {
    return sysDeptService.updateDept(id, param);
  }

  /**
   * <p>todo 导出部门信息</p>
   *
   * @param response
   * @param param
   * @return: void
   * @author: fengliye@expservice.com.cn
   * @date: 2022/6/23 16:21
   * @version: 1.0.1
   */
  @ApiOperation(value = "导出->导出部门信息", notes = "导出部门信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "response", value = "response", dataTypeClass = HttpServletResponse.class),
      @ApiImplicitParam(name = "params", value = "params", dataTypeClass = String.class)
  })
  @PostMapping(value = "/exportDept")
  public void exportDept(HttpServletResponse response, @RequestBody Map<String, Object> param) throws IOException
  {
    AssertMyUtil.notNull(param, BizCode.VARIABLE_NOT_EMPTY, "param");
    sysDeptService.exportDept(response, "部门信息导出", param);
  }

  /**
   * <p>todo 更新部门启用状态</p>
   *
   * @param params
   * @return: java.util.Map<java.lang.String, java.lang.Object>
   * @author: fengliye@expservice.com.cn
   * @date: 2022/6/23 18:02
   * @version: 1.0.1
   */
  @ApiOperation(value = "更新->更新部门启用状态", notes = "更新部门启用状态")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "params", value = "params", dataTypeClass = String.class)
  })
  @MarkLog(value = "更新部门状态-[{0}]", param = "dname")
  @PostMapping(value = "/updateDeptStatus")
  public Map<String, Object> updateDeptStatus(@RequestBody Map<String, Object> params)
  {
    Long id = Long.parseLong(MyUtil.getValue(params.get("id")));
    Integer status = Integer.parseInt(MyUtil.getValue(params.get("status")));
    return sysDeptService.updateDeptStatus(id, status);
  }

  /**
   * <p>todo 门店认证时更新部门pid</p>
   *
   * @author: sunwanzhong
   * @date: 2023/3/3 15:30
   * @version: 1.0.1
   */
  @ApiOperation(value = "更新->部门", notes = "更新部门")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "params", value = "params", dataTypeClass = String.class)
  })
  @PostMapping(value = "/updateDeptFeign")
  @MarkLog(value = "更新部门-[{0}]", param = "params")
  public Map<String, Object> updateDeptFeign(@RequestBody Map<String, Object> params)
  {
    Long id = Long.parseLong(MyUtil.getValue(params.get("id")));
    return sysDeptService.updateDept(id, params);
  }

  /**
   * <p>todo 通过deptid判断是否为经销商</p>
   *
   * @author: sunwanzhong
   * @date: 2023/04/12 17:02
   * @version: 1.0.1
   */
  @ApiOperation(value = "查询->通过deptid判断是否为经销商", notes = "通过deptid判断是否为经销商")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "params", value = "params", dataTypeClass = Map.class)
  })
  @PostMapping(value = "/isDealer")
  @MarkLog(value = "通过deptid判断是否为经销商-[{0}]", param = "params")
  public Map<String, Object> isDealer(@RequestBody Map<String, Object> params)
  {
    return sysDeptService.isDealer(params);
  }
}
